package com.suny.sf_server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suny.Fence.vo.FenceVO;
import com.suny.Pen.dto.PenUpdateStatusDTO;
import com.suny.Pen.vo.PenSelectVO;
import com.suny.sf_server.service.PenService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.suny.Pen.dto.PenAddDTO;
import com.suny.Pen.dto.PenUpdateDTO;
import com.suny.Pen.dto.PenQueryDTO;
import com.suny.Pen.vo.PenVO;
import com.suny.exception.BusinessException;
import com.suny.result.PageDTO;
import com.suny.result.PageVO;
import com.suny.result.R;


import java.util.List;

@Slf4j
@Tag(name = "圈舍管理", description = "圈舍管理相关接口")
@RestController
@RequestMapping("/pen")
public class PenController {
    @Resource
    private PenService penService;
    
    @Operation(summary = "新增圈舍")
    @PostMapping("/add")
    public R add(@Validated @RequestBody PenAddDTO dto) {
        try {
            penService.add(dto);
            return R.OK("新增圈舍成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    
    @Operation(summary = "修改圈舍")
    @PostMapping("/update")
    public R update(@Validated @RequestBody PenUpdateDTO dto) {
        try {
            penService.update(dto);
            return R.OK("修改圈舍成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    //3、圈舍修改时只能修改名称，不能修改所属牧场，且修改后只影响新数据，不影响老数据。
//    @Operation(summary = "更新圈舍信息")
//    @PostMapping("/update")
//    public R update(@RequestBody PenUpdateStatusDTO dto) {
//        try {
//            Integer penId = dto.getPenId();
//            String penName = dto.getPenName();
//            penService.updateInfo(penId, penName);
//            return R.OK("修改圈舍信息成功");
//        } catch (BusinessException e) {
//            return R.FAIL(e.getMessage());
//        }
//    }
    @Operation(summary = "更新圈舍状态")
    @PostMapping("/updateStatus")
    public R updateStatus(@Validated @RequestBody PenUpdateStatusDTO dto) {
        try {
            Integer penId = dto.getPenId();
            Integer status = dto.getStatus();
            penService.updateStatus(penId, status);
            return R.OK("更新状态成功");
        } catch (BusinessException e) {
            return R.FAIL(e.getMessage());
        }
    }
    
//    @Operation(summary = "获取圈舍列表")
//    @PostMapping("/list")
//    public R getList(@Validated @RequestBody PageDTO pageDTO) {
//        try {
//            Page<PenVO> page = penService.getList(pageDTO);
//
//            // 构造返回的分页数据
//            PageVO<PenVO> result = new PageVO<>();
//            result.setCurrent(page.getCurrent());
//            result.setSize(page.getSize());
//            result.setTotal(page.getTotal());
//            result.setPages(page.getPages());
//            result.setRecords(page.getRecords());
//
//            return R.OK(result);
//        } catch (BusinessException e) {
//            return R.FAIL(e.getMessage());
//        }
//    }
    
//    @Operation(summary = "获取牧场下的可用圈舍")
//    @GetMapping("/{farmId}")
//    public R getEnablePensByFarmId(@Parameter(description = "牧场ID")@PathVariable Integer farmId) {
//        try {
//            List<PenVO> list = penService.getEnablePensByFarmId(farmId);
//            return R.OK(list);
//        } catch (BusinessException e) {
//            return R.FAIL(e.getMessage());
//        }
//    }
@Operation(summary = "下拉框获取可用圈舍")
@GetMapping("/select")
public R getSelectByFarmId(@Parameter(description = "牧场ID") Integer farmId) {
    log.info("查询牧场[{}]下的圈舍", farmId);
    try {
        List<PenSelectVO> list = penService.getSelectByFarmId(farmId);
        log.info("查询结果: size={}", list.size());
        return R.OK(list);
    } catch (BusinessException e) {
        log.error("查询失败", e);
        return R.FAIL(e.getMessage());
    }
}
    @Operation(summary = "条件查询圈舍列表")
    @PostMapping("/list")
    public R getListByCondition(@Validated @RequestBody PenQueryDTO query) {
        log.info("接收到查询参数: {}", query);
        try {
            Page<PenVO> page = penService.getListByCondition(query);
            
            // 构造返回的分页数据
            PageVO<PenVO> result = new PageVO<>();
            result.setCurrent(page.getCurrent());
            result.setSize(page.getSize());
            result.setTotal(page.getTotal());
            result.setPages(page.getPages());
            result.setRecords(page.getRecords());
            
            log.info("查询结果: total={}, size={}", page.getTotal(), page.getRecords().size());
            return R.OK(result);
        } catch (BusinessException e) {
            log.error("查询失败", e);
            return R.FAIL(e.getMessage());
        }
    }
}
